#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <Array.au3>
#include "xmpp.au3"
#include "xmpp_exten.au3"

Global $Edit1, $Edit2, $iSocket

; ------------------------------------------[START LOGIN FORM]---------------------------------------------------------
Func xmpp_startlogin()
	#Region ### START Koda GUI section ### Form=c:\users\peter\desktop\datapad\xmpp\xmpplogin_form.kxf
	$xmpplogin_form = GUICreate("XMPP Chat Login", 501, 221, -1, -1)
	$Pic1 = GUICtrlCreatePic("data\launch\xmpp_header.jpg", 0, 0, 500, 100)
	$Group1 = GUICtrlCreateGroup("Login", 8, 104, 489, 57)
	GUICtrlSetFont(-1, 9, 400, 0, "Calibri")
	$Label1 = GUICtrlCreateLabel("Username:", 16, 128, 63, 18)
	$Label2 = GUICtrlCreateLabel("Password:", 296, 128, 59, 18)
	$UsernameInput = GUICtrlCreateInput("UsernameInput", 88, 128, 193, 22)
	$PasswordInput = GUICtrlCreateInput("PasswordInput", 360, 128, 121, 22, BitOR($GUI_SS_DEFAULT_INPUT,$ES_PASSWORD))
	GUICtrlCreateGroup("", -99, -99, 1, 1)
	$Button1 = GUICtrlCreateButton("Login", 8, 192, 75, 25)
	$Button2 = GUICtrlCreateButton("Close", 416, 192, 75, 25)
	$Label3 = GUICtrlCreateLabel("Warning!", 56, 168, 54, 18)
	GUICtrlSetFont(-1, 9, 400, 0, "Calibri")
	GUICtrlSetColor(-1, 0xFF0000)
	$Label4 = GUICtrlCreateLabel("This XMPP client does not encrypt data sent over the internet.", 112, 168, 337, 18)
	GUICtrlSetFont(-1, 9, 400, 0, "Calibri")
	$Checkbox1 = GUICtrlCreateCheckbox("Register Account", 104, 200, 121, 17)
	GUICtrlSetFont(-1, 9, 400, 0, "Calibri")
	$xmpp_help_link = GUICtrlCreateLabel("Help Manual Link", 275, 198, 88, 18)
	GUICtrlSetFont(-1, 9, 400, 2, "Calibri")
	GUICtrlSetColor(-1, 0x0000FF)
	GUICtrlSetCursor (-1, 0)
	GUISetState(@SW_SHOW)
	#EndRegion ### END Koda GUI section ###

	While 1
		$nMsg = GUIGetMsg()
		Switch $nMsg
			Case $GUI_EVENT_CLOSE
				GUIDelete($xmpplogin_form)
				ExitLoop
			Case $Button2
				GUIDelete($xmpplogin_form)
				ExitLoop
			Case $Button1
				xmpp_login(GUICtrlRead($UsernameInput), GUICtrlRead($PasswordInput), GUICtrlRead($Checkbox1))
				GUIDelete($xmpplogin_form)
				xmpp_main()
				ExitLoop
			Case $xmpp_help_link
				OpenURL("http://code.google.com/p/star-datapad/wiki/XMPPChat")
		EndSwitch
	WEnd
EndFunc
; ------------------------------------------[END LOGIN FORM]-----------------------------------------------------------

; ------------------------------------------[START MAIN FORM]----------------------------------------------------------
Func xmpp_main()
	#Region ### START Koda GUI section ### Form=c:\users\peter\desktop\datapad\xmpp\xmppmain_form.kxf
	$xmppmain_form = GUICreate("XMPP Main", 501, 271, -1, -1)
	$Pic1 = GUICtrlCreatePic("data\launch\xmpp_header.jpg", 0, 0, 500, 100)
	$Label1 = GUICtrlCreateLabel("User:", 176, 112, 32, 18)
	GUICtrlSetFont(-1, 9, 400, 0, "Calibri")
	$UserButton1 = GUICtrlCreateButton("Start Chat", 416, 112, 75, 25)
	$Combo1 = GUICtrlCreateCombo("Combo1", 216, 112, 193, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
	$Pic2 = GUICtrlCreatePic("data\launch\xmpp_side.jpg", 0, 100, 164, 170)
	GUISetState(@SW_SHOW)
	#EndRegion ### END Koda GUI section ###

	While 1
		$nMsg = GUIGetMsg()
		Switch $nMsg
			Case $GUI_EVENT_CLOSE
				xmpp_logout()
				GUIDelete($xmppmain_form)
				ExitLoop
			Case $UserButton1
				;MsgBox(0, "", "Starting chat with: "&GUICtrlRead($Combo1))
				xmpp_chat(GUICtrlRead($Combo1))
		EndSwitch
	WEnd
EndFunc
; ------------------------------------------[END MAIN FORM]-------------------------------------------------------------

; ------------------------------------------[START CHAT FORM]-----------------------------------------------------------

Func xmpp_chat($otheruser2)
	#Region ### START Koda GUI section ### Form=c:\users\peter\desktop\datapad\xmpp\xmppchat_form.kxf
	$xmppchat_form = GUICreate("XMPP Chat - "&$otheruser2, 501, 395, 512, 398)
	$Pic1 = GUICtrlCreatePic("data\launch\xmpp_header.jpg", 0, 0, 500, 100)
	$Edit1 = GUICtrlCreateEdit("", 0, 104, 497, 201,BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_VSCROLL))
	;GUICtrlSetData(-1, "Edit1")
	$Edit2 = GUICtrlCreateEdit("", 0, 336, 439, 57, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_VSCROLL))
	GUICtrlSetData(-1, "Hey! How's it going?")
	$Label1 = GUICtrlCreateLabel(" ", 0, 312, 430, 19)
	GUICtrlSetFont(-1, 10, 800, 0, "Calibri")
	$ChatButton1 = GUICtrlCreateButton("Send", 440, 312, 59, 81)
	GUISetState(@SW_SHOW)
	#EndRegion ### END Koda GUI section ###

	While 1
		GetMessages()
		$nMsg = GUIGetMsg()
		Switch $nMsg
			Case $GUI_EVENT_CLOSE
				GUIDelete($xmppchat_form)
				ExitLoop
			Case $ChatButton1
				GUICtrlSetData($Edit1, GUICtrlRead($Edit1)&@CRLF&"Me"&": "&GUICtrlRead($Edit2))
				_XmppSendMessage($otheruser2, GUICtrlRead($Edit2), $iSocket)
				;_XmppSendMessageSubject($otheruser2, GUICtrlRead($Edit2), "Test", $iSocket)
		EndSwitch
	WEnd

EndFunc


; ------------------------------------------[END CHAT FORM]---------------------------------------------------------------

Func xmpp_login($User, $Pass, $Reg)
	; Attempt to connect to server
	; 5222 should be non-tls/non-ssl port for XMPP

	$temp_split = StringSplit($User, "@")

	$connect = _XmppConnect($temp_split[2], 5222)

	If @error Then
		MsgBox(16,"XMPP Connection Error","Unable to connect, error code: "&$connect[3])
		;Exit
	EndIf

	; !<?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='778892135' from='jabber.ccc.de' xml:lang='en'>
	$pos = StringInStr($connect[3], "id='")
	$left = StringTrimLeft($connect[3], $pos + 3)
	$pos = StringSplit($left, "'")
	$id = $pos[1]
	;MsgBox(0, "", $id)


	$iSocket = $connect[1]

	If $Reg = $GUI_CHECKED Then
		$register = _xmppregister($temp_split[1], $Pass, $temp_split[2], 5222)
	EndIf

	If @error Then
		MsgBox(16,"XMPP Registration Error","Unable to register, error code: "&$register)
		Exit
	EndIf
	;MsgBox(0, "X", "X")
	;_XmppAuthenticate($temp_split[1], $Pass, $temp_split[2], $iSocket)
	_XmppAuthenticate2($temp_split[1], $Pass, $id, $temp_split[2], $iSocket)

	If @error Then
		MsgBox(16,"XMPP Authentication Error","Unable to authenticate.")
		Exit
	EndIf

	;_XmppSetGlobalStatus("away", "this is a test", $iSocket)

	;_XmppSendSubscribe("fade@jappix.com", $iSocket)
	;_XmppSendSubscribe2("test888@jabber.ccc.de", "fade@jappix.com", $iSocket)

EndFunc

Func xmpp_logout()
	_XmppDisconnect($iSocket)
	If @error Then MsgBox(16,"D","Unable to logout.")
EndFunc

Func GetMessages()
	$raw_data = _XmppRecive($iSocket)
	If $raw_data[2] = 1 Then
		$sender = $raw_data[4]
		If StringInStr($sender, "/") Then
			$sender = StringLeft($sender, StringInStr($sender, "/") - 1)
		EndIf
		$msg = $raw_data[5]
		GUICtrlSetData($Edit1, GUICtrlRead($Edit1)&@CRLF&$sender&": "&$msg)
	EndIf
EndFunc
